package cn.wei.sdd.core.controller.admin;


import cn.wei.common.exception.Assert;
import cn.wei.common.result.R;
import cn.wei.common.result.ResponseEnum;

import cn.wei.sdd.core.pojo.entity.User;
import cn.wei.sdd.core.pojo.query.UserQuery;
import cn.wei.sdd.core.pojo.vo.AdminHomeVO;
import cn.wei.sdd.core.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author Wei
 * @since 2021-09-22
 */
@Api(tags = "会员管理")
//@CrossOrigin //跨域
@RestController
@RequestMapping("/admin/core/user")
public class AdminUserController {

    @Autowired
    private UserService userService;


    @ApiOperation("根据id删除会员")
    @DeleteMapping("/remove/{id}")
    public R removeById(@PathVariable Long id){
        //如果id=3就手动抛出一个自定义的异常！
        Assert.equals(1,id,ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

        boolean result = userService.removeById(id);
        if (result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }

    @ApiOperation("获取会员分页列表")
    @GetMapping("/list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码",required = true)
            @PathVariable Long page,
            @ApiParam(value = "每页记录数",required = true)
            @PathVariable Long limit,
            @ApiParam(value = "查询对象",required = false)
            UserQuery userQuery){

        Page<User> pageParam = new Page<>(page,limit);
        IPage<User> pageModel = userService.listPage(pageParam,userQuery);

        return R.ok().data("pageModel",pageModel);
    }
    @ApiOperation("锁定和解锁")
    @PutMapping("lock/{id}/{status}")
    public R Lock(
            @ApiParam(value = "用户id",required = true)
            @PathVariable Long id,
            @ApiParam(value = "状态（0：锁定 1：正常）",example = "0",required = true)
            @PathVariable Integer status){

        userService.lock(id,status);
        return R.ok().message(status==1?"解锁成功":"锁定成功");

    }


    @ApiOperation("获取后台管理首页信息对象")
    @GetMapping("getAdminHomeInfo")
    public R getAdminHomeInfo(){
        AdminHomeVO info = userService.getAdminHomeInfo();
        return R.ok().data("info",info);
    }

}

